package org.lanqiao.tjut.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.lanqiao.tjut.bean.UsersEntity;
import org.lanqiao.tjut.service.IUserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserLoginControl {
	// 组合service层，依赖注入
	@Resource
	private IUserInfoService userService;

	@RequestMapping("/login.do")
	public ModelAndView doLogin2(HttpSession session, UsersEntity userE) {
		System.out.println("登录用户名：" + userE.getUsername() + "@" + userE.getUserPsw());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();
		List<UsersEntity> lstUsers = userService.selectUsersInfo4Login(userE);
		if (lstUsers != null && lstUsers.size() > 0) {
			// 登录成功

			// 登录验证成功，将用户信息进行session缓存
			session.setAttribute("LoginedUserInfo", lstUsers.get(0));
			// 设置逻辑视图名--登录成功
			model.setViewName("logined");
		} else {
			// 设置逻辑视图名--登录验证失败
			model.setViewName("login");
		}
		return model;
	}
	
	/**
	 * 测试拦截器用的请求处理
	 * @return
	 */
	@RequestMapping("/test.do")
	public String doTest() {
		return "logined";
	}

}
